package framework.impl;

import framework.api.InternalKnowledgeBase;
import framework.api.KieBaseConfiguration;
import framework.api.KieSessionConfiguration;
import framework.api.KnowledgeBaseFactoryService;

import java.util.Properties;

public class KnowledgeBaseFactory {

    private static KnowledgeBaseFactoryService factoryService;

    private static synchronized KnowledgeBaseFactoryService getKnowledgeBaseFactoryService() {
        if ( factoryService == null ) {
            loadServiceFactory();
        }
        return factoryService;
    }

    public static InternalKnowledgeBase newKnowledgeBase(String kbaseId,
                                                         KieBaseConfiguration conf) {
        return getKnowledgeBaseFactoryService().newKnowledgeBase( kbaseId, conf );
    }

    public static KieSessionConfiguration newKnowledgeSessionConfiguration() {
        return getKnowledgeBaseFactoryService().newKnowledgeSessionConfiguration();
    }
    public static KieBaseConfiguration newKnowledgeBaseConfiguration(Properties properties,
                                                                     ClassLoader... classLoaders) {
        return getKnowledgeBaseFactoryService().newKnowledgeBaseConfiguration( properties,
                classLoaders );
    }

    public static synchronized void setKnowledgeBaseServiceFactory(KnowledgeBaseFactoryService serviceFactory) {
        KnowledgeBaseFactory.factoryService = serviceFactory;
    }
    private static void loadServiceFactory() {
        setKnowledgeBaseServiceFactory( ServiceRegistryImpl.getInstance().get( KnowledgeBaseFactoryService.class ) );
    }
}
